package cc.eddic.examinationsystem.domain;

import cc.eddic.examinationsystem.core.BaseEntity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.*;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.List;

@Entity
@EntityListeners(AuditingEntityListener.class)
@Table(name = "t_exam")
@Builder
@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class Exam extends BaseEntity {
    public Exam(String name, LocalDateTime examAt, Integer timeLimit, Integer totalScore) {
        this.name = name;
        this.examAt = examAt;
        this.timeLimit = timeLimit;
        this.totalScore = totalScore;
    }

    @Column(length = 50)
    private String name;

    private LocalDateTime examAt;

    private Integer timeLimit;
    private Integer totalScore;


    @JsonIgnore
    @ToString.Exclude
    @OneToMany(mappedBy = "exam", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private List<ExamQuestion> questions;

    @JsonIgnore
    @ToString.Exclude
    @OneToMany(mappedBy = "exam", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private List<ExamStudent> students;


    @JsonIgnore
    @ToString.Exclude
    @OneToMany(mappedBy = "exam", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private List<ExamScore> scores;


    @JsonIgnore
    @ToString.Exclude
    @OneToMany(mappedBy = "exam",cascade = CascadeType.ALL,fetch = FetchType.LAZY)
    private List<Answer> answers;

    public Exam add(Answer answer){
        answer.setExam(this);
        answers.add(answer);
        return this;
    }
}
